﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Core.Log
{
    public class LogHelper
    {
        private static string logPath = string.Empty;
        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"\Logs\";
                    if (!Directory.Exists(logPath))
                    {
                        Directory.CreateDirectory(logPath);
                    }
                    //    // Web 应用
                    //    logPath = AppDomain.CurrentDomain.BaseDirectory + @"bin\";
                }
                return logPath;
            }
            set { logPath = value; }
        }

        private static string _logFielPrefix = string.Empty;
        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get { return _logFielPrefix; }
            set { _logFielPrefix = value; }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, string msg)
        {
            try
            {
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    LogPath + LogFielPrefix + logFile + " " +
                    DateTime.Now.ToString("yyyyMMdd") + ".Log"
                    );
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                sw.Close();
            }
            catch
            { }
        }
        public static void WriteLog(string logType,string logFile, string msg)
        {
            try
            {
                logPath = AppDomain.CurrentDomain.BaseDirectory + @"\Logs\"+ logType+@"\";
                if (!Directory.Exists(logPath))
                {
                    Directory.CreateDirectory(logPath);
                }
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    LogPath + LogFielPrefix + logFile + " " +
                    DateTime.Now.ToString("yyyyMMdd") + ".Log"
                    );
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                sw.Close();
            }
            catch
            { }
        }
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogFile logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }
        /// <summary>
        /// 写警告日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Warning(string msg)
        {
            WriteLog(LogFile.Warning.ToString(), msg);
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg)
        {
            WriteLog(LogFile.Error.ToString(), msg);
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg, string logType)
        {
            if (!string.IsNullOrEmpty(logType))
            {
                WriteLog(logType, LogFile.Error.ToString(), msg);
            }
            else
            {
                Error(msg);
            }
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Error(Exception ex)
        {
            WriteLog(LogFile.Error.ToString(), ex.Message + "\r\n" + ex.StackTrace);
        }
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Error(Exception ex, string logType)
        {
           
            if (!string.IsNullOrEmpty(logType))
            {
                WriteLog(logType, LogFile.Error.ToString(), ex.Message + "\r\n" + ex.StackTrace);
            }
            else
            {
                Error(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        /// <summary>
        /// 写一般日志记录
        /// </summary>
        public static void Info(string msg)
        {
            WriteLog(LogFile.Info.ToString(), msg);
        }
        /// <summary>
        /// 写一般日志记录
        /// </summary>
        public static void Info(string msg,string logType)
        {
            if (!string.IsNullOrEmpty(logType))
            {
                WriteLog(logType, LogFile.Info.ToString(), msg);
            }
            else
            {
                Info(msg);
            }
        }
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Info,
        Trace,
        Warning,
        Error,
        Sql
    }
}